package com.ruoyi.service.impl;

import com.ruoyi.domain.SpaceRecord;
import com.ruoyi.dto.DynamicRecord;
import com.ruoyi.dto.PersonalDocInfo;
import com.ruoyi.dto.SpaceDocumentDetail;
import com.ruoyi.mapper.SpaceRecordMapper;
import com.ruoyi.platform.service.impl.BaseServiceImpl;
import com.ruoyi.service.ISpaceRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.List;

/**
 * 智联空间文档操作记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-12-19
 */
@Service
public class SpaceRecordServiceImpl extends BaseServiceImpl<SpaceRecordMapper, SpaceRecord> implements ISpaceRecordService {

    @Autowired
    private SpaceRecordMapper spaceRecordMapper;

    /**
     * 查询智联空间文档操作记录
     *
     * @param id 智联空间文档操作记录主键
     * @return 智联空间文档操作记录
     */
    @Override
    public SpaceRecord selectSpaceRecordById(Long id) {
        return spaceRecordMapper.selectSpaceRecordById(id);
    }

    /**
     * 查询智联空间文档操作记录列表
     *
     * @param spaceRecord 智联空间文档操作记录
     * @return 智联空间文档操作记录
     */
    @Override
    public List<SpaceRecord> selectSpaceRecordList(SpaceRecord spaceRecord) {
        return spaceRecordMapper.selectSpaceRecordList(spaceRecord);
    }

    /**
     * 新增智联空间文档操作记录
     *
     * @param spaceRecord 智联空间文档操作记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSpaceRecord(SpaceRecord spaceRecord) {
        return spaceRecordMapper.insert(spaceRecord);
    }

    /**
     * 修改智联空间文档操作记录
     *
     * @param spaceRecord 智联空间文档操作记录
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSpaceRecord(SpaceRecord spaceRecord) {
        return spaceRecordMapper.updateById(spaceRecord);
    }

    /**
     * 批量删除智联空间文档操作记录
     *
     * @param ids 需要删除的智联空间文档操作记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpaceRecordByIds(Long[] ids) {
        return spaceRecordMapper.deleteSpaceRecordByIds(ids);
    }

    /**
     * 删除智联空间文档操作记录信息
     *
     * @param id 智联空间文档操作记录主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSpaceRecordById(Long id) {
        return spaceRecordMapper.deleteSpaceRecordById(id);
    }

    @Override
    public List<SpaceRecord> getOperateCountRank(Long type, String timeSpan, LocalDate startTime, LocalDate endTime,Integer currentPage) {
        switch (timeSpan) {
            case "本周":
                startTime = LocalDate.now().with(DayOfWeek.MONDAY);
                endTime = LocalDate.now().with(DayOfWeek.SUNDAY);
                break;
            case "本月":
                startTime = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
                endTime = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
                break;
            case "全年":
                startTime = LocalDate.now().with(TemporalAdjusters.firstDayOfYear());
                endTime = LocalDate.now().with(TemporalAdjusters.lastDayOfYear());
                break;
            default:
                startTime = null;
                endTime = null;
                break;
        }
        return spaceRecordMapper.selectOperateCountRank(type, startTime, endTime,currentPage);
    }


    @Override
    public SpaceDocumentDetail getDocStatInfoByDocumentId(Long documentId) {
        return spaceRecordMapper.selectDocStatInfoByDocumentId(documentId);
    }

    @Override
    public List<PersonalDocInfo> getRecordByUserId(String userId, String recordType,String documentName,Integer page) {
        return spaceRecordMapper.selectRecordByUserId(userId, recordType,documentName,page);
    }

    @Override
    public List<DynamicRecord> selectDynamicRecord(Integer page) {
        return spaceRecordMapper.selectDynamicRecord(page);
    }

    @Override
    public List<SpaceRecord> getUploadRank(Long type, String timeSpan, LocalDate startTime, LocalDate endTime) {
        if(startTime != null && endTime != null){
            return spaceRecordMapper.selectUploadRank(type, startTime, endTime);
        }

        switch (timeSpan) {
            case "本周":
                startTime = LocalDate.now().with(DayOfWeek.MONDAY);
                endTime = LocalDate.now().with(DayOfWeek.SUNDAY);
                break;
            case "本月":
                startTime = LocalDate.now().with(TemporalAdjusters.firstDayOfMonth());
                endTime = LocalDate.now().with(TemporalAdjusters.lastDayOfMonth());
                break;
            case "全年":
                startTime = LocalDate.now().with(TemporalAdjusters.firstDayOfYear());
                endTime = LocalDate.now().with(TemporalAdjusters.lastDayOfYear());
                break;
            default:
                startTime = null;
                endTime = null;
                break;
        }
        return spaceRecordMapper.selectUploadRank(type, startTime, endTime);
    }

    @Override
    public Integer getTotal(String userId, String recordType,String documentName) {
        return spaceRecordMapper.getTotal(userId,recordType,documentName);
    }


}
